<script setup lang="ts">
import { FriendLinkApplyFrom } from '@/api/friendLink/type'
import { friendLinkApply } from '@/api/friendLink'

const applyDialogVisible = ref(false)

const applyFormRef = ref(ElForm)

const linkApplyForm = ref(<FriendLinkApplyFrom>{
  name: '',
  avatar: '',
  url: '',
  summary: '',
  email: ''
})

// 申请表单字段格式
const applyFormRules = reactive({
  name: [{ required: true, message: '请正确填写网站名称', trigger: 'blur' }],
  avatar: [{ required: true, message: '请填写网站头像', trigger: 'blur' }],
  url: [{ required: true, message: '请填写网站地址', trigger: 'blur' }],
  summary: [{ required: true, message: '请填写网站简介', trigger: 'blur' }],
  email: [
    {
      type: 'email',
      required: true,
      message: '请填写网站邮箱',
      trigger: 'blur'
    }
  ]
})

// 点击弹出申请框体
const applyLink = () => {
  applyDialogVisible.value = true
  // 表单重置 字段 + 格式校验
  applyFormRef.value.resetFields()
  applyFormRef.value.clearValidate()
}

// 提交表单
function submitApplyForm() {
  applyFormRef.value.validate(async (valid: boolean) => {
    // 如果验证通过
    if (valid) {
      friendLinkApply(linkApplyForm.value).then(({ data: res }) => {
        if (res.code == 200) {
          ElMessage.success('申请已经提交，请耐心等待管理员审核')
          applyDialogVisible.value = false
        } else {
          ElMessage.warning(res.msg)
        }
      })
    }
  })
}
</script>

<template>
  <div class="format-container">
    <div class="title">
      <div>
        <svg-icon icon-class="link" />
        申请格式
      </div>
      <el-button type="primary" @click="applyLink">
        <el-icon>
          <Plus />
        </el-icon>
        加入友链
      </el-button>
    </div>
    <div class="format-group">
      <span>博客名称：C-blog</span>
      <span>博客简介：个人博客</span>
      <span>
        博客头像：
        <a href="https://q.qlogo.cn/headimg_dl?dst_uin=2714957825&spec=100">
          https://q.qlogo.cn/headimg_dl?dst_uin=2714957825&spec=100
        </a>
      </span>
      <span>博客地址：<a href="http://localhost:4000/">http://localhost:4000/</a></span>
    </div>
  </div>

  <!--友链申请弹窗-->
  <el-dialog v-model="applyDialogVisible" class="apply-dialog" title="申请友链" center>
    <template #default>
      <el-form ref="applyFormRef" :model="linkApplyForm" :rules="applyFormRules" label-width="auto">
        <el-form-item label="网站名称" prop="name">
          <el-input v-model="linkApplyForm.name" />
        </el-form-item>
        <el-form-item label="网站简介" prop="summary">
          <el-input v-model="linkApplyForm.summary" />
        </el-form-item>
        <el-form-item label="网站地址" prop="url">
          <el-input v-model="linkApplyForm.url" />
        </el-form-item>
        <el-form-item label="网站头像" prop="avatar">
          <el-input v-model="linkApplyForm.avatar" />
        </el-form-item>
        <el-form-item label="邮箱地址" prop="email">
          <el-input v-model="linkApplyForm.email" />
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="applyDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitApplyForm"> 确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">
.format-container {
  margin: 20px 0;

  .format-group {
    border-left: 5px solid royalblue;
    background-color: #1d72f320;

    span {
      padding: 5px 10px;
      display: block;
    }
  }
}

.title {
  font-weight: 700;
  font-size: 21px;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
}
</style>

<style>
.apply-dialog {
  border-radius: 10px;
}

@media screen and (max-width: 759px) {
  .el-dialog {
    --el-dialog-width: 90%;
  }
}

@media screen and (min-width: 760px) {
  .el-dialog {
    --el-dialog-width: 30%;
  }
}
</style>
